
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>


int ForState=0;
int IfState=0;
int ThenState=0;
int ElseState=0;
int WhileState=0;
int DoState=0;
int LetterState=0;
int DigitState=0;
int DigitDotState = 0;
int JiaState = 0;
int JianState = 0;
int ChenState = 0;
int ChuState = 0;
int MaoState = 0;
int MaodenState = 0;
int XiaoState = 0;
int XiaodaState = 0;
int XiaodenState = 0;
int DaState = 0;
int DadenState = 0;
int DenState = 0;
int FenState = 0;
int ZuokuoState = 0;
int YoukuoState = 0;
int SharpState = 0;
int cleanSpace = 1;
int zhuanYi = 0;


int state = 2;//1 ÎªÊý×Ö  2 Îª×Ö·û  3ÎªÆäËû

char* LetterStr;
char* DigitStr;

char* myCat(char* a, char* b)
{
	int length = 0;
	int ca = 0;
	if (a!=NULL)
	{
		while (a[ca] != '\0')
		{
			length++;
			ca++;
		}
	}
	int cb = 0;
	if (b!=NULL)
	{
		while (b[cb] != '\0')
		{
			length++;
			cb++;
		}
	}
	char* temp = (char*)malloc(sizeof(char)*length + 1);
	int i;
	for (i = 0; i <= length; i++)
	{
		temp[i] = '\0';
	}
	if (a!=NULL)
	{
		strcat(temp, a);

	}
	if (b!=NULL)
	{
		strcat(temp, b);
	}
	return temp;
}


int IsCharacter(char a)
{
	int temp = (int)a;
	if ((temp>=97&&temp<=122)||(temp>=65&&temp<=90))
	{
		return 1;
	}
	return 0;
}

//in this I think . is also digit
int IsNum(char a)
{
	int temp = (int)a;
	if (temp=='.')
	{
		return 1;
	}
	if (temp>=48&&temp<=57)
	{
		return 1;
	}
	return 0;
}

void CleanState()
{
	ForState = 0;
	IfState = 0;
	ThenState = 0;
	ElseState = 0;
	WhileState = 0;
	DoState = 0;
	LetterState = 0;
	DigitState = 0;
	DigitDotState = 0;
	JiaState = 0;
	JianState = 0;
	ChenState = 0;
	ChuState = 0;
	MaoState = 0;
	MaodenState = 0;
	XiaoState = 0;
	XiaodaState = 0;
	XiaodenState = 0;
	DaState = 0;
	DadenState = 0;
	DenState = 0;
	FenState = 0;
	ZuokuoState = 0;
	YoukuoState = 0;
	SharpState = 0;
	LetterStr = "";
	DigitStr = "";
}

///def


void DoTick(char a)
{
	if (DoState == 0 && a == 'd')
	{
		DoState = 1;
	}
	else if (DoState==1 && a=='o')
	{
		DoState = -1;
	}
	else
	{
		DoState = 0;
	}
}

void DigitTick(char a)
{
	if (DigitState==-2)
	{
		return;
	}
	if (DigitState==-1&&a=='.'&&DigitDotState==1)
	{
		DigitState = -2;
		return;
	}
	if (DigitState==0)
	{
		if (IsNum(a))
		{
			DigitState = -1;
			DigitStr=myCat(DigitStr,&a);
			if (a=='.')
			{
				DigitDotState = 1;
			}
		}
		else
		{
			DigitState = -2;
		}
		return;
	}
	if (DigitState==-1&&IsNum(a))
	{
		DigitStr=myCat(DigitStr, &a);
		return;
	}
	else
	{
		DigitState = -2;
		return;
	}
}

void LetterTick(char a)
{
	if (LetterState==0&&IsNum(a))
	{
		LetterState = -2;//-2±íÊ¾Ê§Åä
		return;
	}
	if ((a=='_'||IsNum(a)||IsCharacter(a))&&LetterState!=-2)
	{
		LetterState = -1;
		LetterStr=myCat(LetterStr,&a);
	}
}

void WhileTick(char a)
{
	if (a=='w'&&WhileState==0)
	{
		WhileState = 1;
	}
	else if(a=='h'&&WhileState==1)
	{
		WhileState = 2;
	}
	else if (a=='i'&&WhileState==2)
	{
		WhileState = 3;
	}
	else if (a=='l'&&WhileState==3)
	{
		WhileState = 4;
	}
	else if (a=='e'&&WhileState==4)
	{
		WhileState = -1;
	}
	else
	{
		WhileState = 0;
	}
}

void ElseTick(char a)
{
	if (a=='e'&&ElseState == 3)
	{
		ElseState = -1;
	}
	else if (a=='l'&&ElseState ==1)
	{
		ElseState = 2;
	}
	else if (a=='s'&&ElseState ==2)
	{
		ElseState = 3;
	}
	else if (a=='e')
	{
		ElseState = 1;
	}
	else
	{
		ElseState = 0;
	}
}


void ThenTick(char a)
{
	if (a=='t'&&ThenState==0)
	{
		ThenState = 1;
	}
	else if (a=='h'&&ThenState==1)
	{
		ThenState = 2;
	}
	else if(a=='e'&&ThenState==2)
	{
		ThenState = 3;
	}
	else if (a=='n'&&ThenState==3)
	{
		ThenState = -1;
	}
	else
	{
		ThenState = 0;
	}
}

void JiaTick(char a)
{
	if (JiaState==0&&a=='+')
	{
		JiaState = -1;
	}
	else
	{
		JiaState = 0;
	}
}

void JianTick(char a)
{
	if (JianState == 0 && a == '-')
	{
		JianState = -1;
	}
	else
	{
		JianState = 0;
	}
}

void ChenTick(char a)
{
	if (ChenState == 0 && a == '*')
	{
		ChenState = -1;
	}
	else
	{
		ChenState = 0;
	}
}

void ChuTick(char a)
{
	if (ChuState == 0 && a == '/')
	{
		ChuState = -1;
	}
	else
	{
		ChuState = 0;
	}
}

void MaoTick(char a)
{
	if (MaoState==0&&a==':')
	{
		MaoState = -1;
	}
	else
	{
		MaoState = 0;
	}
}

void MaodenTick(char a)
{
	if (MaodenState == 0 && a == ':')
	{
		MaodenState = 1;
	}
	else if (MaodenState==1&&a=='=')
	{
		MaodenState = -1;
	}
	else
	{
		MaodenState = 0;
	}
}

void XiaoTick(char a)
{
	if (XiaoState==0&&a=='<')
	{
		XiaoState = -1;
	}
	else
	{
		XiaoState = 0;
	}
}

void XiaodaTick(char a)
{
	if (XiaodaState==0&&a=='<')
	{
		XiaodaState = 1;
	}
	else if (XiaodaState==1&&a=='>')
	{
		XiaodaState = -1;
	}
	else
	{
		XiaodaState = 0;
	}
}

void XiaodenTick(char a)
{
	if (XiaodenState==0&&a=='<')
	{
		XiaodenState = 1;
	}
	else if (XiaodenState==1&&a=='=')
	{
		XiaodenState = -1;
	}
	else
	{
		XiaodenState = 0;
	}
}

void DaTick(char a)
{
	if (DaState==0&&a=='>')
	{
		DaState = -1;
	}
	else
	{
		DaState = 0;
	}
}

void DadenTick(char a)
{
	if (DadenState==0&&a=='>')
	{
		DadenState = 1;
	}
	else if (DadenState==1&&a=='=')
	{
		DadenState = -1;
	}
	else
	{
		DadenState = 0;
	}
}

void DenTick(char a)
{
	if (DenState==0&&a=='=')
	{
		DenState = -1;
	}
	else
	{
		DenState = 0;
	}
}


void FenTick(char a)
{
	if (FenState == 0 && a == ';')
	{
		FenState = -1;
	}
	else
	{
		FenState = 0;
	}
}


void ZuokuoTick(char a)
{
	if (ZuokuoState==0&&a=='(')
	{
		ZuokuoState = -1;
	}
	else
	{
		ZuokuoState = 0;
	}
}

void YoukuoTick(char a)
{
	if (YoukuoState==0&&a==')')
	{
		YoukuoState = -1;
	}
	else
	{
		YoukuoState = 0;
	}
}

void SharpTick(char a)
{
	if (SharpState == 0 && a == '#')
	{
		SharpState = -1;
	}
	else
	{
		SharpState = 0;
	}
}

void IfTick(char a)
{
	if (a=='i'&&IfState==0)
	{
		IfState = 1;
	}
	else if(a=='f'&& IfState==1)
	{
		IfState = -1;
	}
	else
	{
		IfState = 0;
	}
}


void ForTick(char a)
{
	if (a=='f'&&ForState==0)
	{
		ForState = 1;
	}
	else if (a == 'o'&&ForState == 1)
	{
		ForState = 2;
	}
	else if (a=='r'&&ForState==2)
	{
		ForState = -1; //-1 indicate success
	}
	else
	{
		ForState = 0;
	}
}


void MyLexProcess(char a)
{
	ForTick(a);
	IfTick(a);
	ThenTick(a);
	ElseTick(a);
	WhileTick(a);
	DoTick(a);
	LetterTick(a);
	DigitTick(a);
	JiaTick(a);
	JianTick(a);
	ChenTick(a);
	ChuTick(a);
	MaoTick(a);
	MaodenTick(a);
	XiaoTick(a);
	XiaodaTick(a);
	XiaodenTick(a);
	DaTick(a);
	DadenTick(a);
	DenTick(a);
	FenTick(a);
	ZuokuoTick(a);
	YoukuoTick(a);
	SharpTick(a);
}

void CheckAndOut()
{
	if (ForState == -1)
	{
		printf("(1, for)\n");
		return;
	}

	if (IfState == -1)
	{
		printf("(2, if)\n");
		return;
	}

	if (ThenState == -1)
	{
		printf("(3, then)\n");
		return;
	}

	if (ElseState == -1)
	{
		printf("(4, else)\n");
		return;
	}

	if (WhileState == -1)
	{
		printf("(5, while)\n");
		return;
	}

	if (DoState == -1)
	{
		printf("(6, do)\n");
		return;
	}

	if (LetterState == -1)
	{
		printf("(id, %s)\n",LetterStr);
		return;
	}

	if (DigitState == -1)
	{
		printf("(num, %s)\n",DigitStr);
		return;
	}

	if (JiaState == -1)
	{
		printf("(13, ;)\n");
		return;
	}

	if (JianState == -1)
	{
		printf("(14, -)\n");
		return;
	}

	if (ChenState == -1)
	{
		printf("(15, *)\n");
		return;
	}

	if (ChuState == -1)
	{
		printf("(16, /)\n");
		return;
	}

	if (MaodenState == -1)
	{
		printf("(18, :=)\n");
		return;
	}

	if (MaoState == -1)
	{
		printf("(17, :)\n");
		return;
	}


	if (XiaoState == -1)
	{
		printf("(20, <)\n");
		return;
	}

	if (XiaodaState == -1)
	{
		printf("(21, <>)\n");
		return;
	}

	if (XiaodenState == -1)
	{
		printf("(22, <=)\n");
		return;
	}

	if (DaState == -1)
	{
		printf("(23, >)\n");
		return;
	}

	if (DadenState == -1)
	{
		printf("(24, >=)\n");
		return;
	}

	if (DenState == -1)
	{
		printf("(25, =)\n");
		return;
	}

	if (FenState == -1)
	{
		printf("(26, ;)\n");
		return;
	}

	if (ZuokuoState == -1)
	{
		printf("(27, ()\n");
		return;
	}

	if (YoukuoState == -1)
	{
		printf("(28, ))\n");
		return;
	}

	if (SharpState == -1)
	{
		printf("(0, #)\n");
		return;
	}
}

void MyLex(char a)  //x := 5 ; if ( x < 0 ) then x := 3 ; #
{
	if (a == ' '||a=='\r'||a=='\n'||a=='\t')
	{
		state = 0;
		return;
	}
	int nowState = 0;
	nowState += 2 * IsCharacter(a);
	nowState += IsNum(a);
	if (nowState == 0)
	{
		cleanSpace = 1;
		nowState = 3;
	}

	if (state == nowState&&a!='#')
	{
		MyLexProcess(a);
	}
	else
	{
		CheckAndOut();
		CleanState();
		MyLexProcess(a);
	}
	if (a=='#')
	{
		CheckAndOut();
	}
	state = nowState;
}

void Fix(FILE *fp)
{
	while (1)
	{
		char x = getc(fp);
		if (x==EOF)
		{
			return;
		}
		MyLex(x);
	}
	//MyLex(x);
}

int main(int argc, const char *argv[])
{
	if (argc < 2) {
		printf("insufficient args\n");
		return 1;
	}

	FILE *fp = fopen(argv[1], "r");
	Fix(fp);

	fclose(fp);
	return 0;
}